package com.frozenlab.android.wheretoeatai;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

public class FoodPlaceEdit extends Activity {
	/* Database helper */
	private FoodPlaceDbHelper dbHelper;
	
	/* Variable for input */
    private Long rowId;
    private EditText inputName;
	private CheckBox checkBoxBreakfast;
	private CheckBox checkBoxLunch;
	private CheckBox checkBoxDinner;
	private CheckBox checkBoxSupper;
	private CheckBox checkBoxRefreshment;
	private CheckBox checkBoxOthers;
	private Spinner spinnerPriceRange;
		
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        dbHelper = new FoodPlaceDbHelper(this);
        dbHelper.open();
        setContentView(R.layout.food_place_edit);
    	   
        Bundle extras = getIntent().getExtras();
        rowId = extras.getLong(FoodPlaceDbHelper.KEY_ROWID);
        
        inputName = (EditText) findViewById(R.id.inputName);
    	checkBoxBreakfast = (CheckBox) findViewById(R.id.checkBoxBreakfast);
    	checkBoxLunch = (CheckBox) findViewById(R.id.checkBoxLunch);
    	checkBoxDinner = (CheckBox) findViewById(R.id.checkBoxDinner);
    	checkBoxSupper = (CheckBox) findViewById(R.id.checkBoxSupper);
    	checkBoxRefreshment = (CheckBox) findViewById(R.id.checkBoxRefreshment);
    	checkBoxOthers = (CheckBox) findViewById(R.id.checkBoxOthers);
        spinnerPriceRange = (Spinner) findViewById(R.id.spinnerPriceRange);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.price_range_array, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnerPriceRange.setAdapter(adapter);
        
    	populateFields();
    }
    
    private void populateFields() {
        if (rowId != null) {
            Cursor cursor = dbHelper.readRecord(rowId);
            startManagingCursor(cursor);
            inputName.setText(cursor.getString(cursor.getColumnIndexOrThrow(FoodPlaceDbHelper.KEY_NAME)));
            if(cursor.getString(cursor.getColumnIndexOrThrow(FoodPlaceDbHelper.KEY_IS_BREAKFAST)).compareTo("1")==0){
            	checkBoxBreakfast.setChecked(true);
            }else{checkBoxBreakfast.setChecked(false);}
            if(cursor.getString(cursor.getColumnIndexOrThrow(FoodPlaceDbHelper.KEY_IS_LUNCH)).compareTo("1")==0){
            	checkBoxLunch.setChecked(true);
            }else{checkBoxLunch.setChecked(false);}
            if(cursor.getString(cursor.getColumnIndexOrThrow(FoodPlaceDbHelper.KEY_IS_DINNER)).compareTo("1")==0){
            	checkBoxDinner.setChecked(true);
            }else{checkBoxDinner.setChecked(false);}
            if(cursor.getString(cursor.getColumnIndexOrThrow(FoodPlaceDbHelper.KEY_IS_SUPPER)).compareTo("1")==0){
            	checkBoxSupper.setChecked(true);
            }else{checkBoxSupper.setChecked(false);}
            if(cursor.getString(cursor.getColumnIndexOrThrow(FoodPlaceDbHelper.KEY_IS_REFRESHMENT)).compareTo("1")==0){
            	checkBoxRefreshment.setChecked(true);
            }else{checkBoxRefreshment.setChecked(false);}
            if(cursor.getString(cursor.getColumnIndexOrThrow(FoodPlaceDbHelper.KEY_IS_OTHERS)).compareTo("1")==0){
            	checkBoxOthers.setChecked(true);
            }else{checkBoxOthers.setChecked(false);}
            spinnerForLoop:
            for(int i=0; i<spinnerPriceRange.getAdapter().getCount(); i++){
            	if(spinnerPriceRange.getAdapter().getItem(i).toString().compareTo(
            			cursor.getString(cursor.getColumnIndexOrThrow(FoodPlaceDbHelper.KEY_PRICE_RANGE)))==0){
            		spinnerPriceRange.setSelection(i);
            		break spinnerForLoop;
            	}
            }
        }
    }
	
    public void onBackClick(View button){	
    	super.onBackPressed();
    }
    
    public void onSaveClick(View button){	
    	inputName = (EditText) findViewById(R.id.inputName);
    	checkBoxBreakfast = (CheckBox) findViewById(R.id.checkBoxBreakfast);
    	checkBoxLunch = (CheckBox) findViewById(R.id.checkBoxLunch);
    	checkBoxDinner = (CheckBox) findViewById(R.id.checkBoxDinner);
    	checkBoxSupper = (CheckBox) findViewById(R.id.checkBoxSupper);
    	checkBoxRefreshment = (CheckBox) findViewById(R.id.checkBoxRefreshment);
    	checkBoxOthers = (CheckBox) findViewById(R.id.checkBoxOthers);
    	spinnerPriceRange = (Spinner) findViewById(R.id.spinnerPriceRange);
    	
    	String name = inputName.getText().toString();
    	String isBreakfast = checkBoxBreakfast.isChecked()? "1":"0";
    	String isLunch = checkBoxLunch.isChecked()? "1":"0";
    	String isDinner = checkBoxDinner.isChecked()? "1":"0";
    	String isSupper = checkBoxSupper.isChecked()? "1":"0";
    	String isRefreshment = checkBoxRefreshment.isChecked()? "1":"0";
    	String isOthers = checkBoxOthers.isChecked()? "1":"0";
    	String priceRange = spinnerPriceRange.getSelectedItem().toString();
    	dbHelper.updateRecord(rowId, name, isBreakfast, isLunch, isDinner, isSupper, isRefreshment, isOthers, priceRange);
    	Toast.makeText(getApplicationContext(), R.string.Food_place_saved, Toast.LENGTH_SHORT).show();
    	finish();
    }
	
	public void showCancelAlert(){
    	AlertDialog.Builder builder = new AlertDialog.Builder(this);
    	builder.setMessage(R.string.Cancel_alert_message)
    	       .setCancelable(false)
    	       .setPositiveButton(R.string.Yes, new DialogInterface.OnClickListener() {
    	           public void onClick(DialogInterface dialog, int id) {
    	        	   FoodPlaceEdit.super.onBackPressed();
    	           }
    	       })
    	       .setNegativeButton(R.string.No, new DialogInterface.OnClickListener() {
    	           public void onClick(DialogInterface dialog, int id) {
    	                dialog.cancel();
    	           }
    	       });
    	AlertDialog alert = builder.create();
    	alert.show();
	}
}
